home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_ImageMagick.idb / usr / freeware / catman / u_man / cat5 / quantize.Z / quantize
Encoding:
Text File  |  1999-01-26  |  15.8 KB  |  331 lines

  1.  
  2.  
  3.  
  4.      qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))     IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk ((((1111    MMMMaaaayyyy 1111999999994444))))       qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       Quantize - ImageMagick's color reduction algorithm.
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.       ####iiiinnnncccclllluuuuddddeeee <<<<mmmmaaaaggggiiiicccckkkk....hhhh>>>>
  13.  
  14.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  15.       This document    describes how IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk performs color
  16.       reduction on an image.  To fully understand this document,
  17.       you should have a knowledge of basic imaging techniques and
  18.       the tree data    structure and terminology.
  19.  
  20.       For purposes of color    allocation, an image is    a set of _n
  21.       pixels, where    each pixel is a    point in RGB space.  RGB space
  22.       is a 3-dimensional vector space, and each pixel, _p ,    is
  23.       defined by an    ordered    triple of red, green, and bl_iue
  24.       coordinates, (_r , _g ,    _b ).
  25.              _i   _i     _i
  26.       Each primary color component (red, green, or blue)
  27.       represents an    intensity which    varies linearly    from 0 to a
  28.       maximum value, _c   , which corresponds to full saturation of
  29.       that color.  Col_mo_ar_xallocation    is defined over    a domain
  30.       consisting of    the cube in RGB    space with opposite vertices
  31.       at (0,0,0) and (_c   ,_c   ,_c    ).  IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk    requires _c
  32.       = _2_5_5.       _m_a_x    _m_a_x  _m_a_x              _m_a_x
  33.  
  34.       The algorithm    maps this domain onto a    tree in    which each
  35.       node represents a cube within    that domain.  In the following
  36.       discussion, these cubes are defined by the coordinate    of two
  37.       opposite vertices: The vertex    nearest    the origin in RGB
  38.       space    and the    vertex farthest    from the origin.
  39.  
  40.       The tree's root node represents the the entire domain,
  41.       (0,0,0) through (_c   ,_c   ,_c     ).  Each lower    level in the
  42.       tree is generated _mb_ay_xsu_mb_ad_xivi_md_ai_xng one node's cube into    eight
  43.       smaller cubes    of equal size.    This corresponds to bisecting
  44.       the parent cube with planes passing through the midpoints of
  45.       each edge.
  46.  
  47.       The basic algorithm operates in three    phases:
  48.       CCCCllllaaaassssssssiiiiffffiiiiccccaaaattttiiiioooonnnn,,,, RRRReeeedddduuuuccccttttiiiioooonnnn, and AAAAssssssssiiiiggggnnnnmmmmeeeennnntttt.  CCCCllllaaaassssssssiiiiffffiiiiccccaaaattttiiiioooonnnn
  49.       builds a color description tree for the image.  RRRReeeedddduuuuccccttttiiiioooonnnn
  50.       collapses the    tree until the number it represents, at    most,
  51.       is the number    of colors desired in the output    image.
  52.       AAAAssssssssiiiiggggnnnnmmmmeeeennnntttt defines the output    image's    color map and sets
  53.       each pixel's color by    reclassification in the    reduced    tree.
  54.       Our goal is to minimize the numerical    discrepancies between
  55.       the original colors and quantized colors.  To    learn more
  56.       about    quantization error, see    MEASURING COLOR    REDUCTION
  57.       ERROR    later in this document.
  58.  
  59.       CCCCllllaaaassssssssiiiiffffiiiiccccaaaattttiiiioooonnnn begins    by initializing    a color    description
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 12/17/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))     IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk ((((1111    MMMMaaaayyyy 1111999999994444))))       qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))
  71.  
  72.  
  73.  
  74.       tree of sufficient depth to represent    each possible input
  75.       color    in a leaf.  However, it    is impractical to generate a
  76.       fully-formed color description tree in the classification
  77.       phase    for realistic values of    _c   .  If color    components in
  78.       the input image are quantized    t_mo_a_x_k-bit precision, so that
  79.       _c    = _2_k-_1, the tree    would need _k levels below the root
  80.       n_mo_ad_xe to allow    representing each possible input color in a
  81.       leaf.     This becomes prohibitive because the tree's total
  82.       number of nodes is
  83.  
  84.           R k
  85.              i=1 8k
  86.       A complete tree would    require    19,173,961 nodes for _k = _8,
  87.       _c    = _2_5_5.  Therefore, to avoid building a fully populated
  88.       t_mr_ae_xe,    IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk: (1) Initializes data structures for nodes
  89.       only as they are needed; (2) Chooses a maximum depth for the
  90.       tree as a function of    the desired number of colors in    the
  91.       output image (currently _l_o_g (_c_o_l_o_r_m_a_p    _s_i_z_e)+_2).  A tree of
  92.       this depth generally allows_4the best representation of the
  93.       source image with the    fastest    computational speed and    the
  94.       least    amount of memory.  However, the    default    depth is
  95.       inappropriate    for some images.  Therefore, the caller    can
  96.       request a specific tree depth.
  97.  
  98.       For each pixel in the    input image, classification scans
  99.       downward from    the root of the    color description tree.     At
  100.       each level of    the tree, it identifies    the single node    which
  101.       represents a cube in RGB space containing the    pixel's    color.
  102.       It updates the following data    for each such node:
  103.  
  104.       nnnn ::::  Number of pixels    whose color is contained in the    RGB
  105.        1111   cube which this node represents;
  106.  
  107.       nnnn ::::  Number of pixels    whose color is not represented in a
  108.        2222   node at lower depth in the tree;     initially,  _n    = _0
  109.            for all nodes except leaves of the tree.          _2
  110.  
  111.       SSSS ,,,, SSSS    ,,,, SSSS ::::
  112.        rrrr   ggggSumsbbbbof the red,    green, and blue    component values for
  113.            all pixels not classified at a lower depth.  The
  114.            combination of these sums and _n    will ultimately
  115.            characterize the    mean color of _2a    set of pixels
  116.            represented by this node.
  117.  
  118.       EEEE::::   The distance squared in RGB space between each pixel
  119.            contained within    a node and the nodes' center.  This
  120.            represents the quantization error for a node.
  121.  
  122.       RRRReeeedddduuuuccccttttiiiioooonnnn repeatedly prunes the tree until the number    of
  123.       nodes    with _n     > _0 is    less than or equal to the maximum
  124.       number of co_2lors allowed in the output image.     On any    given
  125.       iteration over the tree, it selects those nodes whose    _E
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 12/17/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))     IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk ((((1111    MMMMaaaayyyy 1111999999994444))))       qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))
  137.  
  138.  
  139.  
  140.       value    is minimal for pruning and merges their    color
  141.       statistics upward.  It uses a    pruning    threshold, _E , to
  142.       govern node selection    as follows:            _p
  143.  
  144.         E  = 0
  145.         wphile number of nodes with (n  > 0)    > required maximum
  146.       number of colors         2
  147.         prune all nodes    such that E <= E
  148.         Set E    to minimum E in    remaininpg nodes
  149.              p
  150.       This has the effect of minimizing any    quantization error
  151.       when merging two nodes together.
  152.  
  153.       When a node to be pruned has offspring, the pruning
  154.       procedure invokes itself recursively in order    to prune the
  155.       tree from the    leaves upward.    The values of _n      _S , _S    ,  and
  156.       _S  in    a node being pruned are    always added to_2the_r   _g
  157.       c_borresponding    data in    that node's parent.  This retains the
  158.       pruned node's    color characteristics for later    averaging.
  159.  
  160.       For each node,  _n  pixels exist for which that node
  161.       represents the sm_2allest volume in RGB    space containing those
  162.       pixel's colors.  When    _n   > _0    the node will uniquely define
  163.       a color in the output    i_2mage.    At the beginning of reduction,
  164.       _n  = _0  for all nodes    except the leaves of the tree which
  165.       r_2epresent colors present in the input    image.
  166.  
  167.       The other pixel count, _n ,  indicates    the total number of
  168.       colors within    the cubic _1volume which the node    represents.
  169.       This includes    _n  - _n    pixels whose colors should be defined
  170.       by nodes at a    l_1ower _2level in the tree.
  171.  
  172.       AAAAssssssssiiiiggggnnnnmmmmeeeennnntttt generates the output image    from the pruned    tree.
  173.       The output image consists of two parts:  (1)    A color    map,
  174.       which    is an array of color descriptions (RGB triples)    for
  175.       each color present in    the output image; (2)  A pixel array,
  176.       which    represents each    pixel as an index into the color map
  177.       array.
  178.  
  179.       First, the assignment    phase makes one    pass over the pruned
  180.       color    description tree to establish the image's color    map.
  181.       For each node    with _n    > _0, it    divides    _S , _S ,    and _S  by _n .
  182.       This produces    the me_2an color of all pix_rels _gthat cla_bssify _2no
  183.       lower    than this node.     Each of these colors becomes an entry
  184.       in the color map.
  185.  
  186.       Finally, the assignment phase    reclassifies each pixel    in the
  187.       pruned tree to identify the deepest node containing the
  188.       pixel's color.  The pixel's value in the pixel array becomes
  189.       the index of this node's mean    color in the color map.
  190.  
  191.       Empirical evidence suggests that distances in    color spaces
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 12/17/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))     IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk ((((1111    MMMMaaaayyyy 1111999999994444))))       qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))
  203.  
  204.  
  205.  
  206.       such as YUV, or YIQ correspond to perceptual color
  207.       differences more closely than    do distances in    RGB space.
  208.       These    color spaces may give better results when color
  209.       reducing an image.  Here the algorithm is as described
  210.       except each pixel is a point in the alternate    color space.
  211.       For convenience, the color components    are normalized to the
  212.       range    0 to a maximum value, _c      .  The color reduction can
  213.       then proceed as described.   _m_a_x
  214.  
  215.      MMMMEEEEAAAASSSSUUUURRRRIIIINNNNGGGG CCCCOOOOLLLLOOOORRRR RRRREEEEDDDDUUUUCCCCTTTTIIIIOOOONNNN EEEERRRRRRRROOOORRRR
  216.       Depending on the image, the color reduction error may    be
  217.       obvious or invisible.     Images    with high spatial frequencies
  218.       (such    as hair    or grass) will show error much less than
  219.       pictures with    large smoothly shaded areas (such as faces).
  220.       This is because the high-frequency contour edges introduced
  221.       by the color reduction process are masked by the high
  222.       frequencies in the image.
  223.  
  224.       To measure the difference between the    original and color
  225.       reduced images (the total color reduction error),
  226.       IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk sums over    all pixels in an image the distance
  227.       squared in RGB space between each original pixel value and
  228.       its color reduced value. IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk prints several error
  229.       measurements including the mean error    per pixel, the
  230.       normalized mean error, and the normalized maximum error.
  231.  
  232.       The normalized error measurement can be used to compare
  233.       images.  In general, the closer the mean error is to zero
  234.       the more the quantized image resembles the source image.
  235.       Ideally, the error should be perceptually-based, since the
  236.       human    eye is the final judge of quantization quality.
  237.  
  238.       These    errors are measured and    printed    when ----vvvveeeerrrrbbbboooosssseeee and
  239.       ----ccccoooolllloooorrrrssss _a_r_e _s_p_e_c_i_f_i_e_d    _o_n _t_h_e _c_o_m_m_a_n_d _l_i_n_e:
  240.  
  241.       mmmmeeeeaaaannnn eeeerrrrrrrroooorrrr ppppeeeerrrr ppppiiiixxxxeeeellll::::
  242.            is the mean error for any single    pixel in the image.
  243.  
  244.       nnnnoooorrrrmmmmaaaalllliiiizzzzeeeedddd mmmmeeeeaaaannnn ssssqqqquuuuaaaarrrreeee eeeerrrrrrrroooorrrr::::
  245.            is the normalized mean square quantization error    for
  246.            any single pixel    in the image.
  247.  
  248.            This distance measure is    normalized to a    range between
  249.            0 and 1.     It is independent of the range    of red,    green,
  250.            and blue    values in the image.
  251.  
  252.       nnnnoooorrrrmmmmaaaalllliiiizzzzeeeedddd mmmmaaaaxxxxiiiimmmmuuuummmm ssssqqqquuuuaaaarrrreeee eeeerrrrrrrroooorrrr::::
  253.            is the largest normalized square    quantization error for
  254.            any single pixel    in the image.
  255.  
  256.            This distance measure is    normalized to a    range between
  257.            0 and 1.     It is independent of the range    of red,    green,
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 12/17/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))     IIIImmmmaaaaggggeeeeMMMMaaaaggggiiiicccckkkk ((((1111    MMMMaaaayyyy 1111999999994444))))       qqqquuuuaaaannnnttttiiiizzzzeeee((((9999))))
  269.  
  270.  
  271.  
  272.            and blue    values in the image.
  273.  
  274.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  275.       ddddiiiissssppppllllaaaayyyy((((1111)))),,,, aaaannnniiiimmmmaaaatttteeee((((1111)))),,,, mmmmooooggggrrrriiiiffffyyyy((((1111)))),,,, iiiimmmmppppoooorrrrtttt((((1111)))),,,, mmmmiiiiffffffff((((5555))))
  276.  
  277.      CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  278.       Copyright 1998 E. I. du Pont de Nemours and Company
  279.  
  280.       Permission is    hereby granted,    free of    charge,    to any person
  281.       obtaining a copy of this software and    associated
  282.       documentation    files ("ImageMagick"), to deal in ImageMagick
  283.       without restriction, including without limitation the    rights
  284.       to use, copy,    modify,    merge, publish,    distribute,
  285.       sublicense, and/or sell copies of ImageMagick, and to    permit
  286.       persons to whom the ImageMagick is furnished to do so,
  287.       subject to the following conditions:
  288.  
  289.       The above copyright notice and this permission notice    shall
  290.       be included in all copies or substantial portions of
  291.       ImageMagick.
  292.  
  293.       The software is provided "as is", without warranty of    any
  294.       kind,    express    or implied, including but not limited to the
  295.       warranties of    merchantability, fitness for a particular
  296.       purpose and noninfringement.    In no event shall E. I.    du
  297.       Pont de Nemours and Company be liable    for any    claim, damages
  298.       or other liability, whether in an action of contract,    tort
  299.       or otherwise,    arising    from, out of or    in connection with
  300.       ImageMagick or the use or other dealings in ImageMagick.
  301.  
  302.       Except as contained in this notice, the name of the E. I. du
  303.       Pont de Nemours and Company shall not    be used    in advertising
  304.       or otherwise to promote the sale, use    or other dealings in
  305.       ImageMagick without prior written authorization from the E.
  306.       I. du    Pont de    Nemours    and Company.
  307.  
  308.      AAAACCCCKKKKNNNNOOOOWWWWLLLLEEEEDDDDGGGGEEEEMMMMEEEENNNNTTTTSSSS
  309.       Paul Raveling, USC Information Sciences Institute, for the
  310.       original idea    of using space subdivision for the color
  311.       reduction algorithm.    With Paul's permission,    this document
  312.       is an    adaptation from    a document he wrote.
  313.  
  314.      AAAAUUUUTTTTHHHHOOOORRRRSSSS
  315.       John Cristy, E.I. du Pont de Nemours and Company
  316.       Incorporated
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 12/17/98)
  328.  
  329.  
  330.  
  331.